#
#  Copyright (C) 2009 GSyC/LibreSoft
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 
#    Author : Jose Antonio Santos Cadenas <jcaden __at__ gsyc __dot__ es>
#

from django.contrib.gis.db import models
from social.core.models import Social_node, Group

PRIVACY_CHOICES = (
    (0, u'Private'),
    (1, u'Friends'),
    (2, u'Friends of friends'),
    (3, u'Public'),
)

class Role(models.Model):
    """
    Represents a role. It can be defined by a set of characters
    """
    #Object reference
    name = models.SlugField(unique=True)
    characters = models.ManyToManyField(Social_node)

class Privacy_field(models.Model):
    
    node = models.ForeignKey(Social_node, unique=True)
    field = models.CharField(max_length=20)
    friends_privacy = models.IntegerField(default=0, choices=PRIVACY_CHOICES)
    forbidden_groups = models.ManyToManyField(Group, related_name="forbidden_elements_field", blank=True)
    forbidden_roles = models.ManyToManyField(Role, related_name="forbidden_elements_field", blank=True)
    allowed_groups = models.ManyToManyField(Group, related_name="allowed_elements_field", blank=True)
    allowed_roles = models.ManyToManyField(Role, related_name="allowd_elements_field", blank=True)
    
    def __unicode__(self):
        return "Privacy of %s in %s: %s" % (self.node.type, self.field, self.node.get_node())


class Privacy(models.Model):
    
    node = models.ForeignKey(Social_node, unique=True)
    friends_privacy = models.IntegerField(default=0, choices=PRIVACY_CHOICES)
    forbidden_groups = models.ManyToManyField(Group, related_name="forbidden_elements", blank=True)
    forbidden_roles = models.ManyToManyField(Role, related_name="forbidden_elements", blank=True)
    allowed_groups = models.ManyToManyField(Group, related_name="allowed_elements", blank=True)
    allowed_roles = models.ManyToManyField(Role, related_name="allowd_elements", blank=True)
    
    def __unicode__(self):
        return "Privacy of %s: %s" % (self.node.type, self.node.get_node())
